package com.axon.components.controls
{
	import com.axon.components.containers.AxPanel;
	import com.axon.components.containers.AxTitleWindowSizeable;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.controls.CheckBox;
	import mx.core.Container;

	public class AxCheckBox extends CheckBox
	{
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function AxCheckBox()
		{
			super();
//			addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
//		protected function onKeyDown( event: KeyboardEvent ): void
//		{
//			if ( (event.charCode == Keyboard.ENTER) || (event.charCode == Keyboard.ESCAPE) )
//			{
//				var theParent: DisplayObjectContainer = parent;
//			
//				while ((theParent != null) && !(theParent is AxPanel) && !(theParent is AxTitleWindowSizeable))
//					theParent = theParent.parent;
//					
//				if (theParent != null) 
//					theParent.dispatchEvent(event.clone());
//
//				event.stopImmediatePropagation();
//			}  
//		}

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
    	override protected function keyDownHandler(event: KeyboardEvent):void
    	{
			var theParent: DisplayObjectContainer = parent;
					
    		switch (event.charCode) {
    			
    			case Keyboard.ENTER:
    			
					while ((theParent != null) && !(theParent is AxPanel) && !(theParent is AxTitleWindowSizeable))
						theParent = theParent.parent;
						
					if ( (theParent != null)&& (Container(theParent).defaultButton) )
						Container(theParent).defaultButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
						
					event.preventDefault();
					event.stopImmediatePropagation();
						
					break;
				
				case Keyboard.ESCAPE:
				
					while ((theParent != null) && !(theParent is AxPanel) && !(theParent is AxTitleWindowSizeable))
						theParent = theParent.parent;
						
					if (theParent != null) 
						theParent.dispatchEvent(event.clone());
						
					break;
					
				case Keyboard.SPACE:

					selected = !selected;
				
				default:
    		
					super.keyDownHandler(event);
				
    		}
		}

	}
}